'''
请实现两个函数，分别用来序列化和反序列化二叉树

解题思路：递归法将树化为字符串 然后再将字符串化为二叉树
'''
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None
class Solution:
    def __init__(self):
        self.flag = -1

    def Serialize(self, root):
        # write code here
        if not root:
            return '#-'
        return str(root.val) + '-' + self.Serialize(root.left) + self.Serialize(root.right)

    def Deserialize(self, s):
        self.flag += 1
        cur = s.split('-')
        if self.flag > len(s): return None
        root = None
        if cur[self.flag] != '#':
            root = TreeNode(int(cur[self.flag]))
            root.left = self.Deserialize(s)
            root.right = self.Deserialize(s)
        return root




a8 = TreeNode(8)
a61 = TreeNode(6)
a62 = TreeNode(6)
a51 = TreeNode(5)
a71 = TreeNode(7)
a72 = TreeNode(7)
a52 = TreeNode(5)


a61.left = a51
a61.right = a71
a62.left = a72
a62.right = a52
a8.left = a61
a8.right = a62

s = Solution()
print(s.Serialize(a8))